#include "Mario.h"
#pragma comment(lib,"d3dx9.lib")

Mario::Mario() : MovableObject()
{
	_w = MARIO_WIDTH;
	_h = MARIO_HEIGHT;
	_speedX = (float)SPEED;
	_speedY = 2.5f;
	_moveSprite = NULL;
	_jumpSprite = NULL;
	jumping = false;
	jumpKeyDown = false;
	_type = MarioPlayer;
}

Mario::~Mario()
{
	delete _moveSprite;
	delete _jumpSprite;
}

D3DXVECTOR2 Mario::_tempPosition(0,0);

void Mario::SetPosition(D3DXVECTOR2 position)
{
	_tempPosition = position;
}

void Mario::LoadResources(LPD3DXSPRITE SpriteHandler)
{
	_position = _tempPosition;

	_vx = 0;
	_vx_last = 1.0f;
	_vy = 0;

	LPDIRECT3DDEVICE9 d3ddv; 
	SpriteHandler -> GetDevice(&d3ddv);

	_texture.LoadImage(d3ddv,L"Images//mario.png");

	_moveSprite = new AnimatedSprite(SpriteHandler, &_texture, MARIO_WIDTH, MARIO_HEIGHT, 8, 4, 0);
	_jumpSprite = new Sprite(SpriteHandler, &_texture, MARIO_WIDTH, MARIO_HEIGHT, 4, 8);
}

//pixel 

float Mario::GetLowY()
{
	return _position.y - _h/2;
}
float Mario::GetLeft()
{
	return _position.x - _w/2;
}

int Mario::GetWidth()
{
	return 10;
}
int Mario::GetHeight()
{
	return 10;
}

void Mario::Left()
{
	if(_vx > -SPEED)
		_vx -= SPEED_UNITS_ACC;
	_vx_last = _vx;
}
void Mario::Right()
{
	if(_vx < SPEED)
		_vx += SPEED_UNITS_ACC;
	_vx_last = _vx;
}
void Mario::Reset()
{
	if(_vx_last > 0)
	{
		_vx -= SPEED_UNITS_ACC;
		if(_vx < 0) _vx = 0;
	}
	else if(_vx_last < 0)
	{
		_vx += SPEED_UNITS_ACC;
		if(_vx > 0) _vx = 0;
	}

	if(_vx <= STOP_ANIMATE_AT_SPEED && _vx >= -STOP_ANIMATE_AT_SPEED)
		_moveSprite -> Reset();
}
void Mario::Jump()
{
	_vy = _speedY;
}

void Mario::Update(int Delta, vector<GameObject*> vectorObj, QRectangle bound)
{
	MovableObject::Update(Delta, vectorObj, bound);
	if(_state == hurt)
	{
		_vy += 3;
		_position.y += (float)(_vy * Delta);
	}
	else
	{
		// Jump if not already jumping and the jump key was released earlier
		if (GetAsyncKeyState(' ') && !jumping && !jumpKeyDown)
		{
			jumping = true;
			jumpKeyDown = true;
			Jump();
		}

		// Jump key released
		if (!GetAsyncKeyState(' '))
			jumpKeyDown = false;

		if(_position.x < MARIO_WIDTH/2) 
		{
			_position.x = MARIO_WIDTH/2;
			_vx = 0;
		}
		else if(_position.x > ViewPort::MapWidth() - MARIO_WIDTH/2)
		{
			_position.x = (float)ViewPort::MapWidth() - MARIO_WIDTH/2;
			_vx = 0;
		}

		// Animate player if she is running
		DWORD now = GetTickCount();
		if (now - last_time > 1000 / ANIMATE_RATE)
		{
			if ((_vx > STOP_ANIMATE_AT_SPEED || _vx < -STOP_ANIMATE_AT_SPEED)
				&& _position.x > MARIO_WIDTH/2 && _position.x < ViewPort::MapWidth() - MARIO_WIDTH/2) 
				_moveSprite -> Next();
			last_time = now;
		}
		
	}
}

void Mario::RenderFrame(LPDIRECT3DSURFACE9 BackBuffer, ViewPort *viewport)
{
	//which frame?
	if(jumping)
	{
		if (_vx_last > 0)
		{
			_jumpSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, 1, 1, viewport);
		}
		else if (_vx_last < 0)
			_jumpSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, -1, 1, viewport);
	}
	else if (_vx > 0)
	{
		_moveSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, 1, 1, viewport);
	}
	else if (_vx < 0)
		_moveSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, -1, 1, viewport);
	else //_vx=0
		if (_vx_last < 0)
			_moveSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, -1, 1, viewport);
		else 
			_moveSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, 1, 1, viewport);
}

D3DXVECTOR2 Mario::GetPosition()
{
	return D3DXVECTOR2(_position);
}